package com.xinyou.u8c.generator;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.poi.ss.formula.functions.T;

/**
 * @Author: pxm
 * @Description: TODO
 * @CreateTime: 2023-04-04  10:15
 * @Version: 1.0
 */
public class OidBaseAlgorithm {
    private static final byte MIN_CODE = 48;

    private static final byte MAX_CODE = 90;

    private final int CODE_LENGTH = 14;

    private byte[] oidBaseCodes = new byte[14];

    private final  String START_KEY = "0000OO";

    private final  String COUNT_KEY = "10000000000000";

    private void setOidBaseCodes(byte[] oidBaseCodes) {
        if (oidBaseCodes.length != 14)
            return;
        System.arraycopy(oidBaseCodes, 0, this.oidBaseCodes, 0, 14);
    }

    public static synchronized OidBaseAlgorithm getInstance() {
        OidBaseAlgorithm oidBase = new OidBaseAlgorithm();
        return oidBase;
    }

    public static OidBaseAlgorithm getInstance(String strOidBase) {
        OidBaseAlgorithm oidBase = new OidBaseAlgorithm();
        oidBase.setOidBaseCodes(strOidBase.getBytes());
        return oidBase;
    }

    public String nextOidBase() {
        for (int i = this.oidBaseCodes.length - 1; i >= 0; i--) {
            byte code = (byte)(this.oidBaseCodes[i] + 1);
            boolean carryUp = false;
            byte newCode = code;
            if (code > 90) {
                newCode = 48;
                carryUp = true;
            }
            if (newCode == 58)
                newCode = 65;
            this.oidBaseCodes[i] = newCode;
            if (!carryUp)
                break;
        }
        return new String(this.oidBaseCodes);
    }

    /**
     * @description: 获取主键
     * @author: pxm
     * @date: 2023/4/6 14:28
     * @param: [baseMapper, 表名, 主键字段]
     * @return: java.lang.String
     **/
    public String getOid(BaseMapper<T> baseMapper,String tableName,String filedName){


        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq(filedName,);

        String oidBaseCode =null;
        boolean b=false;
        OidBaseAlgorithm gen = getInstance(COUNT_KEY);
        do{
            T t = baseMapper.selectOne(queryWrapper.orderByDesc(filedName));
            if(t == null){
                oidBaseCode = gen.nextOidBase();
                t = baseMapper.selectById(START_KEY + oidBaseCode);
                if(t == null){
                    b = true;
                }
            }else{
                //待补充
                b = true;
            }
        }while (!b);
        return oidBaseCode;
    }
    public static void main(String[] args) {
        OidBaseAlgorithm gen = getInstance("10000000000000");
        long time = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            String oidBaseCode = gen.nextOidBase();
            //if (oidBaseCode.endsWith("RS"))
                System.out.println(oidBaseCode);
        }
        System.out.println(System.currentTimeMillis() - time);
    }
}